package com.leyou.common.advice;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.leyou.common.exceptions.LyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author 虎哥
 */
@Slf4j
@ControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(LyException.class)
    public ResponseEntity<String> handleException(LyException e){
        // 返回异常结果
        return ResponseEntity.status(e.getStatus()).body(e.getMessage());
    }

    @ExceptionHandler(MybatisPlusException.class)
    public ResponseEntity<String> handleException(MybatisPlusException e){
        // 返回异常结果
        log.error(e.getMessage());
        return ResponseEntity.status(400).body("服务器迷失在茫茫人海之中，让我静静！");
    }

    @ExceptionHandler(BindException.class)
    public ResponseEntity<String> handleException(BindException e){
        // 返回异常结果
        log.error(e.getMessage());
        return ResponseEntity.status(400).body("放过我吧！——来自服务器的求救信号");
    }

}
